home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLSRC.PAK / IMAGELST.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  12.3 KB  |  506 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.11  $
  6. //
  7. // Implementation of class TImageList, an ImageList 'common control' wrapper
  8. //----------------------------------------------------------------------------
  9. #include <owl/pch.h>
  10. #if !defined(OWL_IMAGELST_H)
  11. # include <owl/imagelst.h>
  12. #endif
  13.  
  14. OWL_DIAGINFO;
  15. DIAG_DECLARE_GROUP(OwlCommCtrl);    // Common Controls diagnostic group
  16.  
  17. //
  18. // Construct an empty ImageList of a given size
  19. //
  20. TImageList::TImageList(const TSize& size, uint flags, int initCount, int growBy)
  21. {
  22.   if (!TCommCtrl::IsAvailable())
  23.     TXCommCtrl::Raise();
  24.  
  25. //  if (!(flags & ILC_COLOR))
  26. //    flags |= ILC_COLORDDB;
  27.  
  28.   if (!initCount)
  29.     initCount = 1;
  30.  
  31.   ImageSize = TSize(size.cx/initCount, size.cy);
  32.   Bitmap = 0;
  33.   Handle = TCommCtrl::Dll()->ImageList_Create(size.cx, size.cy, flags, initCount, growBy);
  34.  
  35.   CheckValid();
  36.   WARNX(OwlCommCtrl, !Handle, 0, "Cannot create ImageList");
  37. }
  38.  
  39. //
  40. // Construct a TImageList from a bitmap, slicing a portion of the bitmap up
  41. // into a horizontal array of specified sized images.
  42. //
  43. TImageList::TImageList(const TBitmap& bmp, uint flags, int initCount, int growBy)
  44. {
  45.   if (!TCommCtrl::IsAvailable())
  46.     TXCommCtrl::Raise();
  47.  
  48.   if (!initCount)
  49.     initCount = 1;
  50.  
  51.   ImageSize = TSize(bmp.Width() / initCount, bmp.Height());
  52.   Bitmap = 0;
  53.  
  54. //  if (!(flags & 0xFE))
  55. //    flags |= ILC_COLORDDB;         // default color type?
  56. //  flags |= ILC_MASK;
  57.  
  58.   Handle = TCommCtrl::Dll()->ImageList_Create(ImageSize.cx, ImageSize.cy,
  59.                                        flags, initCount, growBy);
  60.  
  61.   CheckValid();
  62.   WARNX(OwlCommCtrl, !Handle, 0, "Cannot create ImageList");
  63.  
  64. #if   defined(BI_PLAT_WIN32)
  65.   // Use masked support with 3dFace color as background color
  66.   //
  67.   Add(bmp, TColor::Sys3dFace);  
  68. #elif defined(BI_PLAT_WIN16)
  69.   // With no mask support, simply add the bitmap
  70.   //
  71.   Add(bmp);
  72. #endif  
  73. }
  74.  
  75. //
  76. // Construct a TImageList from a dib, slicing the dib up into a horizontal array
  77. // of even sized images
  78. //
  79. TImageList::TImageList(const TDib& dib, uint flags, int initCount, int growBy)
  80. {
  81.   if (!TCommCtrl::IsAvailable())
  82.     TXCommCtrl::Raise();
  83.  
  84.   if (!initCount)
  85.     initCount = 1;
  86.  
  87.   ImageSize = TSize(dib.Width() / initCount, dib.Height());
  88.   Bitmap = 0;
  89.  
  90. //  if (!(flags & 0xFE))
  91. //    flags |= ILC_COLORDDB;         // default color type?
  92. //  flags |= ILC_MASK;
  93.  
  94.   Handle = TCommCtrl::Dll()->ImageList_Create(ImageSize.cx, ImageSize.cy,
  95.                                        flags, initCount, growBy);
  96.  
  97. #if   defined(BI_PLAT_WIN32)
  98.   // Use masked support with 3dFace color as background color
  99.   //
  100.   Add(TBitmap(dib, &TPalette((HPALETTE)::GetStockObject(DEFAULT_PALETTE))),
  101.       TColor::Sys3dFace);
  102. #elif defined(BI_PLAT_WIN16)
  103.   // With no mask support, simply add the bitmap
  104.   //
  105.   Add(TBitmap(dib, &TPalette((HPALETTE)::GetStockObject(DEFAULT_PALETTE))));
  106. #endif
  107. }
  108.  
  109. #if defined(BI_PLAT_WIN32)
  110.  
  111. //
  112. // Construct a ImageList right from a bmp, icon or cursor resource in a file.
  113. // 'type' should be one of the consts from winuser.h:
  114. //   IMAGE_BITMAP
  115. //   IMAGE_ICON
  116. //   IMAGE_CURSOR
  117. //   IMAGE_ENHMETAFILE ?
  118. //
  119. TImageList::TImageList(HINSTANCE hI, TResId resName, int w, int growBy,
  120.                        const TColor& mask, uint type, uint flags)
  121. {
  122.   if (!TCommCtrl::IsAvailable())
  123.     TXCommCtrl::Raise();
  124.  
  125.   ImageSize = TSize(0); 
  126.   Bitmap = 0;
  127.   Handle = TCommCtrl::Dll()->ImageList_LoadImage(hI, resName, w, growBy, mask, type, flags);
  128.  
  129.   CheckValid();
  130.   WARNX(OwlCommCtrl, !Handle, 0, "Cannot create ImageList");
  131. }
  132.  
  133.  
  134. //
  135. // Construct a TImageList as a copy of an existing one.
  136. //
  137. TImageList::TImageList(const TImageList& src)
  138. {
  139.   if (!TCommCtrl::IsAvailable())
  140.     TXCommCtrl::Raise();
  141.  
  142.   TImageInfo ii(src);
  143.  
  144.   // Initialize members using ii
  145.   //
  146.   ImageSize = ii.GetImageRect().Size();
  147.   Bitmap = 0;
  148.   Handle = TCommCtrl::Dll()->ImageList_Create(ImageSize.cx, ImageSize.cy, ILC_COLORDDB,
  149.                                        src.GetImageCount(), 10);
  150.  
  151.   CheckValid();
  152.   WARNX(OwlCommCtrl, !Handle, 0, "Cannot create ImageList");
  153.  
  154.   TCommCtrl::Dll()->ImageList_Add(Handle, ii.GetImageBM(), ii.GetMaskBM());
  155. }
  156.  
  157. #endif  //  BI_PLAT_WIN32
  158.  
  159. //
  160. // Construct a C++ alias for an existing imagelist
  161. //
  162. TImageList::TImageList(HIMAGELIST imageList)
  163. {
  164.   if (!TCommCtrl::IsAvailable())
  165.     TXCommCtrl::Raise();
  166.  
  167.   Handle = imageList;
  168.   Bitmap = 0;
  169.   int cx = 0;
  170.   int cy = 0;
  171.  
  172. #if defined(BI_PLAT_WIN32)
  173.   TCommCtrl::Dll()->ImageList_GetIconSize(Handle, &cx, &cy);
  174. #endif
  175.  
  176.   ImageSize.cx = cx;
  177.   ImageSize.cy = cy;
  178.  
  179.   CheckValid();
  180.   WARNX(OwlCommCtrl, !Handle, 0, "Cannot create ImageList");
  181. }
  182.  
  183. #if defined(BI_PLAT_WIN32)
  184. //
  185. // Constructs a wrapper for the current drag imagelist and
  186. // specifies the location and hotspot of the imagelist.
  187. //
  188. TImageList::TImageList(TPoint& pt, TPoint& hotspot)
  189. {
  190.   ImageSize = TSize(0); 
  191.   Bitmap = 0;
  192.   Handle = TCommCtrl::Dll()->ImageList_GetDragImage(&pt, &hotspot);
  193.  
  194.   CheckValid();
  195.   WARNX(OwlCommCtrl, !Handle, 0, "Cannot create ImageList");
  196. }
  197. #endif  //  BI_PLAT_WIN32
  198.  
  199. //
  200. // Destruct the ImageList and clean up the image list handle.
  201. //
  202. TImageList::~TImageList()
  203. {
  204.   TCommCtrl::Dll()->ImageList_Destroy(Handle);
  205.   delete Bitmap;
  206. }
  207.  
  208. //
  209. // Throw an exception if this image list handle is invalid
  210. //
  211. void
  212. TImageList::CheckValid()
  213. {
  214.   if (!Handle)
  215.     TXCommCtrl::Raise();
  216. }
  217.  
  218.  
  219. #if defined(BI_PLAT_WIN32)
  220.  
  221. //
  222. // Assign a ImageList over this ImageList, replacing all contents
  223. //
  224. TImageList&
  225. TImageList::operator =(const TImageList& src)
  226. {
  227.   TCommCtrl::Dll()->ImageList_Remove(Handle, -1);  // -1 is to remove all
  228.  
  229.   TImageInfo ii(src);
  230.   TCommCtrl::Dll()->ImageList_Add(Handle, ii.GetImageBM(), ii.GetMaskBM());
  231.  
  232.   return *this;
  233. }
  234.  
  235. //
  236. // Return the image bitmap used by this ImageList
  237. //
  238. TImageList::operator TBitmap&()
  239. {
  240.   // Build the bitmap object on the fly if needed
  241.   //
  242.   TImageInfo ii(*this);
  243.   if (!Bitmap || (HBITMAP)*Bitmap != ii.GetImageBM()) {
  244.     delete Bitmap;
  245.     Bitmap = new TBitmap(TScreenDC(), TDib(TBitmap(ii.GetImageBM())));
  246.   }
  247.  
  248.   return *Bitmap;
  249. }
  250.  
  251. #endif  //  BI_PLAT_WIN32
  252.  
  253.  
  254. //
  255. // Draw an image onto a target DC at a given coordinate & with a given style
  256. //
  257. bool
  258. TImageList::Draw(int index, TDC& dc, int x, int y, uint style, int overlay)
  259. {
  260.   if (overlay)
  261.     style |= ILD_OVERLAYMASK | INDEXTOOVERLAYMASK(overlay);
  262.   return TCommCtrl::Dll()->ImageList_Draw(Handle, index, dc, x, y, style);
  263. }
  264.  
  265. #if defined(BI_PLAT_WIN32)
  266.  
  267. //
  268. // Extended version of draw that takes a foreground color and background color
  269. //
  270. bool
  271. TImageList::Draw(int index, TDC& dc, int x, int y, int dx, int dy,
  272.                  const TColor& bgClr, const TColor& fgClr, uint style,
  273.                  int overlay)
  274. {
  275.   if (overlay)
  276.     style |= ILD_OVERLAYMASK | INDEXTOOVERLAYMASK(overlay);
  277.   COLORREF bgCr = (bgClr == TColor::None) ? (COLORREF)CLR_NONE : (COLORREF)bgClr;
  278.   COLORREF fgCr = (fgClr == TColor::None) ? (COLORREF)CLR_NONE : (COLORREF)fgClr;
  279.  
  280.   return TCommCtrl::Dll()->ImageList_DrawEx(Handle, index, dc, x, y, dx, dy, bgCr, fgCr, style);
  281. }
  282. #endif  //  BI_PLAT_WIN32
  283.  
  284. //
  285. // Return number of images currently in this ImageList
  286. //
  287. int
  288. TImageList::GetImageCount() const
  289. {
  290.   return TCommCtrl::Dll()->ImageList_GetImageCount(Handle);
  291. }
  292.  
  293. //
  294. // Add new image(s) to the ImageList - return index of new addition.
  295. // No mask bitmap is added.
  296. //
  297. int
  298. TImageList::Add(const TBitmap& image)
  299. {
  300.   return TCommCtrl::Dll()->ImageList_Add(Handle, image, 0);
  301. }
  302.  
  303.  
  304. //
  305. // Add new image/mask pair(s) to the ImageList - return index of new
  306. // addition.
  307. //
  308. int
  309. TImageList::Add(const TBitmap& image, const TBitmap& mask)
  310. {
  311.   return TCommCtrl::Dll()->ImageList_Add(Handle, image, mask);
  312. }
  313.  
  314. #if defined(BI_PLAT_WIN32)
  315.  
  316. //
  317. // Add new image(s) to ImageList, specifying a mask color to generate a mask.
  318. // return index of the new addition.
  319. //
  320. int
  321. TImageList::Add(const TBitmap& image, const TColor& mskColor)
  322. {
  323.   return TCommCtrl::Dll()->ImageList_AddMasked(Handle, image, mskColor);
  324. }
  325. #endif  //  BI_PLAT_WIN32
  326.  
  327. //
  328. // Add an icon to the ImageList - return index of new addition.
  329. //
  330. int
  331. TImageList::Add(const TIcon& icon)
  332. {
  333.   return TCommCtrl::Dll()->ImageList_AddIcon(Handle, icon);
  334. }
  335.  
  336. #if defined(BI_PLAT_WIN32)
  337. //
  338. // Remove an image (or all images if index is -1) from this ImageList.
  339. //
  340. bool
  341. TImageList::Remove(int index)
  342. {
  343.   return TCommCtrl::Dll()->ImageList_Remove(Handle, index);
  344. }
  345.  
  346. //
  347. // Replace an image in this ImageList.
  348. //
  349. bool
  350. TImageList::Replace(int index, const TBitmap& image)
  351. {
  352.   return TCommCtrl::Dll()->ImageList_Replace(Handle, index, image, 0);
  353. }
  354.  
  355. //
  356. // Replace an image and mask in the ImageList.
  357. //
  358. bool
  359. TImageList::Replace(int index, const TBitmap& image, const TBitmap& mask)
  360. {
  361.   return TCommCtrl::Dll()->ImageList_Replace(Handle, index, image, mask);
  362. }
  363.  
  364. //
  365. // Create and retreive an icon from an image and mask in the ImageList.
  366. //
  367. HICON
  368. TImageList::GetIcon(int index, uint flags) const
  369. {
  370.   return TCommCtrl::Dll()->ImageList_GetIcon(Handle, index, flags);
  371. }
  372. #endif  //  BI_PLAT_WIN32
  373.  
  374. //
  375. // Replaces the image at index 'index' with the icon or cursor.
  376. //
  377. int
  378. TImageList::ReplaceIcon(int index, HICON icon)
  379. {
  380.   return TCommCtrl::Dll()->ImageList_ReplaceIcon(Handle, index, icon);
  381. }
  382.  
  383. #if defined(BI_PLAT_WIN32)
  384. //
  385. // Return the icon size
  386. //
  387. bool
  388. TImageList::GetIconSize(int& cx, int& cy)
  389. {
  390.   return TCommCtrl::Dll()->ImageList_GetIconSize(Handle, &cx, &cy);
  391. }
  392.  
  393. //
  394. // Get general information about a given image
  395. //
  396. bool
  397. TImageList::GetImageInfo(int index, TImageInfo& imageInfo) const
  398. {
  399.   return TCommCtrl::Dll()->ImageList_GetImageInfo(Handle, index, &(IMAGEINFO)imageInfo);
  400. }
  401. #endif  //  BI_PLAT_WIN32
  402.  
  403. //
  404. // Get the current background color for this ImageList
  405. //
  406. TColor
  407. TImageList::GetBkColor() const
  408. {
  409.   return TCommCtrl::Dll()->ImageList_GetBkColor(Handle);
  410. }
  411.  
  412. //
  413. // Set the current background color for this ImageList, returning the previous
  414. // color.
  415. //
  416. TColor
  417. TImageList::SetBkColor(const TColor& color)
  418. {
  419.   return TCommCtrl::Dll()->ImageList_SetBkColor(Handle, color);
  420. }
  421.  
  422. //
  423. // Select an image for use as an overlay. Up to four can be selected.
  424. //
  425. bool
  426. TImageList::SetOverlayImage(int index, int overlay)
  427. {
  428.   return TCommCtrl::Dll()->ImageList_SetOverlayImage(Handle, index, overlay);
  429. }
  430.  
  431. #if defined(BI_PLAT_WIN32)
  432.  
  433. //
  434. // Combine the current drag image with another image in the list.
  435. // Typically a mouse cursor would be added to the image list and merged
  436. // with the drag image list.
  437. //
  438. bool
  439. TImageList::SetDragCursorImage(int drag, int dxHotspot, int dyHotspot)
  440. {
  441.   return TCommCtrl::Dll()->ImageList_SetDragCursorImage(Handle, drag, dxHotspot, dyHotspot);
  442. }
  443.  
  444. //
  445. // BeginDrag sets this imagelist to be the drag imagelist.
  446. // There can only be one drag imagelist at any time.
  447. //
  448. bool
  449. TImageList::BeginDrag(int index, int dxHotspot, int dyHotspot)
  450. {
  451.   return TCommCtrl::Dll()->ImageList_BeginDrag(Handle, index, dxHotspot, dyHotspot);
  452. }
  453.  
  454. //
  455. // Typically, this function is called in response to a WM_LBUTTONDOWN message.
  456. // The 'x' and 'y' parameters are relative to the upper-left corner of the
  457. // window's rectangle and NOT the client area.
  458. // The window 'hWndLock' is locked from further updates.
  459. //
  460. bool
  461. TImageList::DragEnter(HWND hWndLock, int x, int y)
  462. {
  463.   return TCommCtrl::Dll()->ImageList_DragEnter(hWndLock, x, y);
  464. }
  465.  
  466. //
  467. // DragMove is typically called when receiving a WM_MOUSEMOVE message.
  468. // The 'x' and 'y' parameters are generally passed from the message to this
  469. // function.
  470. //
  471. bool
  472. TImageList::DragMove(int x, int y)
  473. {
  474.   return TCommCtrl::Dll()->ImageList_DragMove(x, y);
  475. }
  476.  
  477. //
  478. // DragLeave is typically called when receiving a WM_LBUTTONUP message.
  479. // The 'hWndLock' window is unlocked from updates.
  480. //
  481. bool
  482. TImageList::DragLeave(HWND hWndLock)
  483. {
  484.   return TCommCtrl::Dll()->ImageList_DragLeave(hWndLock);
  485. }
  486.  
  487. //
  488. // EndDrag removes the current drag imagelist from the system.
  489. //
  490. void
  491. TImageList::EndDrag()
  492. {
  493.   TCommCtrl::Dll()->ImageList_EndDrag();
  494. }
  495.  
  496. //
  497. // Lock or unlocks the window from updates.
  498. //
  499. bool
  500. TImageList::DragShowNolock(bool show)
  501. {
  502.   return TCommCtrl::Dll()->ImageList_DragShowNolock(show);
  503. }
  504.  
  505. #endif
  506.